package com.etime.perfectschool.controller;

import com.etime.perfectschool.pojo.FoodPhoto;
import com.etime.perfectschool.pojo.NewsPhoto;
import com.etime.perfectschool.service.FoodPhotoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
public class FoodPhotoController {
    @Autowired
    private FoodPhotoService foodPhotoService ;
    @PostMapping("/insertFoodPhoto")
    public int insertFoodPhoto(MultipartFile[] file, int foodId)
    {
        int i =0;
        String path = "D:\\perfectschool1\\src\\main\\resources\\static\\schoolnews";
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        try {
            for (MultipartFile multipartFile:file) {
                multipartFile.transferTo(new File(file1,multipartFile.getOriginalFilename()));
                String photoName = multipartFile.getOriginalFilename();
                System.out.println(photoName);
                FoodPhoto foodPhoto=new FoodPhoto(0,photoName,foodId);
                 i = foodPhotoService.insertFoodPhoto(foodPhoto);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }

}
